package nl.haemers.testrunners 
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.FPSMeter;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.List;
	import com.bit101.components.ListItem;
	import com.bit101.components.PushButton;
	import com.bit101.components.Text;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import nl.haemers.timer.SubTime;
	
	import com.bit101.components.TextArea;
	
	import nl.haemers.base.BaseSprite;
	import nl.haemers.timer.StopWatch;
	
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class StopWatchRunner extends BaseSprite
	{
		public var stopwatch				:StopWatch;
		
		public var startBtn					:PushButton;
		public var resetBtn					:PushButton;
		public var stopBtn					:PushButton;
		public var lapTimeBtn				:PushButton;
		public var fpsMeter					:FPSMeter;
		public var lapTimeList				:List;
		public var currentList				:List;
		public var revertCheck				:CheckBox;
		public var setTimeText				:InputText;
		public var maxTimeText				:InputText;
		
		public var logField					:TextArea;
		public var margin					:int = 10;
		
		public function StopWatchRunner() 
		{
			super();
		}
		
		public function start() : void
		{
			createControls();
			stopwatch = new StopWatch();
			configListeners();
			//stopwatch.start();
		}
		
		/** Protected methods **/
		protected function configListeners() : void
		{
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			startBtn.addEventListener(MouseEvent.CLICK, startHandler);
			resetBtn.addEventListener(MouseEvent.CLICK, resetHandler);
			stopBtn.addEventListener(MouseEvent.CLICK, stopHandler);
			lapTimeBtn.addEventListener(MouseEvent.CLICK, laptimeHandler);
		}
		
		protected function enterFrameHandler(e:Event) : void
		{
			var seconds:int = stopwatch.currentTime;// 1000;
			
			logField.text = seconds.toString() + "\n" + logField.text;
		}
		
		protected function startHandler(e:MouseEvent) : void 
		{
			e.stopImmediatePropagation();
			stopwatch.start();
		}
		
		protected function resetHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			stopwatch.reset();
			reset();
		}
		
		protected function reset() : void
		{
			lapTimeList.removeAll();
			currentList.removeAll();
		}
		
		protected function stopHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			stopwatch.stop();
		}
		
		protected function laptimeHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			
			var subTime:SubTime = stopwatch.getSubTime();
			currentList.addItemAt(subTime.currentTime, 0);
			lapTimeList.addItemAt(subTime.subTime, 0);
		}

		protected function createControls() : void
		{
			fpsMeter = new FPSMeter(this, 500, 50);
			
			logField = new TextArea(this, 20, 200, "test");
			logField.enabled = true;
			
			lapTimeList = new List(this, logField.x + logField.width + margin, 200);
			var txtLabel:Label = new Label(this, lapTimeList.x, lapTimeList.y - 20, "LapTime");
			
			currentList = new List(this, lapTimeList.x + lapTimeList.width + margin, 200);
			txtLabel = new Label(this, currentList.x, currentList.y - 20, "Current Time");
			
			startBtn = new PushButton(this, 20, 50, "Start", startHandler);
			resetBtn = new PushButton(this, startBtn.x + startBtn.width + margin, 50, "Reset", resetHandler);
			stopBtn = new PushButton(this, resetBtn.x + resetBtn.width + margin, 50, "Stop", stopHandler);
			lapTimeBtn = new PushButton(this, stopBtn.x + stopBtn.width + margin, 50, "Laptime", laptimeHandler);
			
			revertCheck = new CheckBox(this, startBtn.x, startBtn.y + startBtn.height + margin, "Revert", revertHandler);
			setTimeText = new InputText(this, revertCheck.x, revertCheck.y + revertCheck.height + margin, "10000", setTimeHandler);
			
		}
		
		protected function setTimeHandler(e:Event) : void
		{
			e.stopImmediatePropagation();
			stopwatch.countDownTime = parseFloat(setTimeText.text);
		}
		
		protected function revertHandler(e:Event) : void
		{
			e.stopImmediatePropagation();
			stopwatch.countDown = revertCheck.selected;
		}
		
	}

}